package com.wenan.ui.grids
{
	import mx.controls.listClasses.IListItemRenderer;
	
	import spark.components.BorderContainer;
	import spark.components.Label;
	
	public class DataReportValueItemRender extends Label implements IListItemRenderer
	{
		private var _column:DataReportValueColumn;
		private var _data:Object = null;
		
		public function DataReportValueItemRender()
		{
			super();
			
			setStyle("verticalAlign", "middle");
			setStyle("paddingLeft", "8");
		}
		
		public function get column():DataReportValueColumn
		{
			return _column;
		}

		public function set column(value:DataReportValueColumn):void
		{
			_column = value;
		}

		public function get data():Object
		{
			return _data;
		}
		
		/**
		 * 初始化数据（增加列、dataField发生变化等操作）时，同步控件。
		 */
		public function set data(value:Object):void
		{
			_data = value;

			if (_data != null)
			{
				var fieldData:Object = _data[_column.dataField];
				if (isSimple(fieldData))
				{
					this.text =  _data[_column.dataField];
				}
				else
				{
					this.text =  _data[_column.dataField].value;
					
					setStyle("backgroundColor", fieldData.color);	
				}
			}
		} 
		
		private function isSimple(value:Object):Boolean
		{
			var type:String = typeof(value);
			switch (type)
			{
				case "number":
				case "string":
				case "boolean":
				{
					return true;
				}
					
				case "object":
				{
					return (value is Date) || (value is Array);
				}
			}
			
			return false;
		}
	}
}